<?php
namespace Admin\Controller;
use Think\Controller;
class RoleController extends IndexController {
    
    public function lists(){
        $this->display();
    }
    public function role_list(){
        $role=D("role");
        if(I("request.iDisplayStart")==''){
            $start=0;
        }else{
            $start=I("request.iDisplayStart");
        }
        if(I("request.sSearch")!=''){
            $where['name'] = array('like','%'.I("request.sSearch").'%');;
        }else{
            $where='';
        }

        $result=$role->field("id,name,abs")
            ->where($where)
            ->limit($start,I("request.iDisplayLength"))
            ->select();
        if(!$result) {
            echo json_encode(0);exit;
        }

        $ret = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $start,
            "iTotalDisplayRecords" => $role->where($where)->count(),
            "aaData" => $result
        );
        
        echo json_encode($ret);
    }
    public function manager($id=''){
        setcookie("roleid", $id, time()+3600);
        $this->display();
    }
    public function manager_list(){
        $model=D("Model");
        $role=D("role");
        $res=$model->where("cd_id=0")->select();
        if(!$res){
            echo json_encode(0);exit;
        }
        if(!isset($_COOKIE['roleid'])){
            echo json_encode(0);exit;
        }
        $ress=$role->where('id='.$_COOKIE['roleid'])->field("model_id")->find();
        if(!$ress){
            echo json_encode(0);exit;
        }
        foreach($res as &$v){
            $r=$model->where("cd_id=".$v['id'])->field("id,name")->select();
            if($r!=null){
                $arr=explode(",", rtrim($ress['model_id'],","));
                foreach($r as &$vv){
                    if(in_array($vv['id'], $arr)){
                        $vv['isc'] =1;      
                    }else{
                        $vv['isc'] =0; 
                    }
                }
                $v['index']=$r;
                
            }
            unset($v['ico']);
            unset($v['is_cd']);
            unset($v['cd_id']);
            unset($v['controller']);
            unset($v['function']);
        }
        echo json_encode($res);
    }
    
    public function update_manager($model_id=''){
        $role=D("role");
        if(!isset($_COOKIE['roleid'])){
            echo json_encode(0);exit;
        }
        $data['model_id']=$model_id;
        $ress=$role->where('id='.$_COOKIE['roleid'])->save($data);
        if(!$ress){
            echo json_encode(0);exit;
        }
        echo json_encode(1);exit;
    }
    public function update(){
        $_SESSION['role_id']=I("request.id");
        $this->display();
    }
    public function updates($type){
        $role=D("role");
        
        $name=I("request.name");
        $abs=I("request.abs");
        if($type==1){
            $role->add(array('name'=>$name,'abs'=>$abs));
        }else{
            if($_SESSION['role_id']==null){
                echo json_encode(0);
            }
            $id=$_SESSION['role_id'];
            $_SESSION['role_id']=NULL;
            $role->where("id=".$id)->save(array('name'=>$name,'abs'=>$abs));
        }
        $this->redirect("Role/lists");
    }
    public function update_list(){
        $role=D("role");
        if($_SESSION['role_id']==null){
            echo json_encode(0);
        }
        $id=$_SESSION['role_id'];
        $rr=$role->where("id=".$id)->find();
        echo json_encode($rr);
    }
    public function add(){
        $this->display();
    }
}